package tq.com.tqteacher.message.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.message.model.TQNoticeModel;

public class TQNoticeAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQNoticeModel> mNoticeModels = new ArrayList<>();

    public TQNoticeAdapter(Context pContext){
        mContext = pContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, int pI) {
        CommonViewHolder lViewHolder = CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_notice_item_layout, pViewGroup);
        lViewHolder.setHolderBinder(new CommonViewHolder.IViewHolder() {
            @Override
            public void bind(View pView, final Object data) {
                TextView lContent = pView.findViewById(R.id.content);
                TextView lFrom = pView.findViewById(R.id.from);

                if(data instanceof TQNoticeModel){
                    TQNoticeModel lModel = (TQNoticeModel)data;
                    lContent.setText(lModel.getContent());

                    lFrom.setText(String.format("%s %s", lModel.getFrom(), lModel.getDate()));
                }

                pView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
                    @Override
                    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
                        menu.add("删除").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                if(mItemClickListener != null){
                                    mItemClickListener.deleteNotice((TQNoticeModel) data);
                                }
                                return false;
                            }
                        });
                    }
                });
            }
        });

        lViewHolder.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mItemClickListener != null){
                    mItemClickListener.clickItem();
                }
            }
        });
        return lViewHolder;
    }

    @Override
    public int getItemCount() {
        return mNoticeModels.size();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        TQNoticeModel lNoticeModel = mNoticeModels.get(pI);

        ((CommonViewHolder)pViewHolder).bindData(lNoticeModel);
    }

    public void delNotice(TQNoticeModel pNoticeModel){
        int index = mNoticeModels.indexOf(pNoticeModel);
        mNoticeModels.remove(pNoticeModel);
        notifyItemRemoved(index);
    }

    public void insertNotice(int pos, TQNoticeModel pNoticeModel){
        mNoticeModels.add(pos, pNoticeModel);
        notifyItemInserted(pos);
    }

    public void append(ArrayList<TQNoticeModel> pArrayList){
        mNoticeModels.addAll(pArrayList);
        notifyDataSetChanged();
    }

    public void replace(ArrayList<TQNoticeModel> pArrayList){
        mNoticeModels.clear();
        mNoticeModels.addAll(pArrayList);
        notifyDataSetChanged();
    }


    private IItemClickListener mItemClickListener;
    public void setItemClickListener(IItemClickListener pListener){
        mItemClickListener = pListener;
    }
    public interface IItemClickListener{
        void clickItem();
        void deleteNotice(TQNoticeModel pNoticeModel);
    }
}
